<?php
 
function load_support_delivery($region_id,$order_id)
{
$region_id =intval($region_id);
$user_info = es_session::get('user_info');
if($order_id==0)
{
$forbid_deliverys_res  = $GLOBALS['db']->getAll('select dd.delivery_id as did from '.DB_PREFIX.'deal_delivery as dd left join '.DB_PREFIX."deal_cart as dc on dd.deal_id = dc.deal_id where dc.session_id = '".es_session::id()."' and dc.user_id = ".intval($user_info['id']));
}
else
{
$forbid_deliverys_res  = $GLOBALS['db']->getAll('select dd.delivery_id as did from '.DB_PREFIX.'deal_delivery as dd left join '.DB_PREFIX.'deal_order_item as doi on dd.deal_id = doi.deal_id left join '.DB_PREFIX.'deal_order as do on doi.order_id = do.id where do.id = '.$order_id);
}
$forbid_delivery = array();
foreach ($forbid_deliverys_res as $row)
{
array_push($forbid_delivery,$row['did']);
}
$support_delivery_list = load_auto_cache('cache_support_delivery',$region_id);
foreach($support_delivery_list as $k=>$row)
{
if(in_array($row['id'],$forbid_delivery))
{
unset($support_delivery_list[$k]);
}
}
return $support_delivery_list;
}
function count_buy_total($region_id,$delivery_id,$payment,$account_money,$all_account_money,$ecvsn,$ecvpassword,$goods_list,$paid_account_money = 0,$paid_ecv_money = 0,$bank_id = '')
{
$pay_price = 0;
$total_price = 0;
$total_weight = 0;
$return_total_score = 0;
$return_total_money = 0;
$is_delivery = 0;
$free_delivery_item = 0;
$deal_id = 0;
$deal_count = 0;
$deal_item_count = 0;
foreach($goods_list as $k=>$v)
{
if($deal_id != $v['deal_id'])
{
$deal_id = $v['deal_id'];
$deal_count = $v['number'];
$deal_item_count ++;
}
else
{
$deal_count += $v['number'];
}
$total_price += $v['total_price'];
$deal_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'deal where id = '.$v['deal_id']);
if($deal_info['is_delivery'] == 1) 
{
if($GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'free_delivery where deal_id = '.$deal_info['id'].' and delivery_id = '.intval($delivery_id).' and free_count <= '.$deal_count))
{
$free_delivery_item+=1;
}
else
{
$deal_weight = floatval($deal_info['weight']);
$deal_weight_unit = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'weight_unit where id = '.$deal_info['weight_id']);
$deal_weight = $deal_weight * $deal_weight_unit['rate'];
$total_weight += ($deal_weight*$v['number']);
}
$is_delivery = 1;
}
$return_total_money = $return_total_money +$deal_info['return_money'] * $v['number'];
$return_total_score = $return_total_score +$deal_info['return_score'] * $v['number'];
}
$region_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'delivery_region where id = '.intval($region_id));
if($region_info['region_level']!=4&&$GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'delivery_region where pid = '.intval($region_id))==0)
{
$region_info['region_level'] = 4;
}
$delivery_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'delivery where id = '.intval($delivery_id));
if($free_delivery_item != $deal_item_count)
$delivery_fee = count_delivery_fee($total_weight,$region_id,intval($delivery_info['id']));
else
$delivery_fee = 0;
$pay_price = $total_price +$delivery_fee;
$pay_price = $pay_price -$paid_account_money -$paid_ecv_money;
$user_id = intval($GLOBALS['user_info']['id']);
$group_info = $GLOBALS['db']->getRow('select g.* from '.DB_PREFIX.'user as u left join '.DB_PREFIX.'user_group as g on u.group_id = g.id where u.id = '.$user_id);
if($group_info&&$total_price>0)
$user_discount = $total_price * (1-floatval($group_info['discount']));
else
$user_discount = 0;
$pay_price = $pay_price -$user_discount;
$user_money = $GLOBALS['db']->getOne('select money from '.DB_PREFIX.'user where id = '.$user_id);
if($all_account_money == 1)
{
$account_money = $user_money;
}
if($account_money>$user_money)
$account_money = $user_money;
$now = get_gmtime();
$ecv_sql = 'select e.* from '.DB_PREFIX.'ecv as e left join '.
DB_PREFIX."ecv_type as et on e.ecv_type_id = et.id where e.sn = '".
$ecvsn."' and e.password = '".
$ecvpassword."' and ((e.begin_time <> 0 and e.begin_time < ".$now.') or e.begin_time = 0) and '.
'((e.end_time <> 0 and e.end_time > '.$now.') or e.end_time = 0) and ((e.use_limit <> 0 and e.use_limit > e.use_count) or (e.use_limit = 0)) '.
'and (e.user_id = '.$user_id.' or e.user_id = 0)';
$ecv_data = $GLOBALS['db']->getRow($ecv_sql);
$ecv_money = $ecv_data['money'];
if($ecv_money +$account_money >$pay_price)
{
if($ecv_money >= $pay_price)
{
$ecv_use_money = $pay_price;
$account_money = 0;
}
else
{
$ecv_use_money = $ecv_money;
$account_money = $pay_price -$ecv_use_money;
}
}
else
{
$ecv_use_money = $ecv_money;
}
$pay_price = $pay_price -$ecv_use_money -$account_money;
if($payment!=0)
{
if($pay_price>0)
{
$payment_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'payment where id = '.$payment);
$directory = APP_ROOT_PATH.'system/payment/';
$file = $directory.'/'.$payment_info['class_name'].'_payment.php';
if(file_exists($file))
{
require_once($file);
$payment_class = $payment_info['class_name'].'_payment';
$payment_object = new $payment_class();
if(method_exists($payment_object,'get_name'))
{
$payment_info['name'] = $payment_object->get_name($bank_id);
}
}
if($payment_info['fee_type']==0) 
{
$payment_fee = $payment_info['fee_amount'];
}
else 
{
$payment_fee = $pay_price * $payment_info['fee_amount'];
}
$pay_price = $pay_price +$payment_fee;
}
}
else
{
$payment_fee = 0;
}
$result = array(
'total_price'=>$total_price,
'pay_price'=>$pay_price,
'pay_total_price'=>$total_price+$delivery_fee+$payment_fee-$user_discount,
'delivery_fee'=>$delivery_fee,
'delivery_info'=>$delivery_info,
'payment_fee'=>$payment_fee,
'payment_info'=>$payment_info,
'user_discount'=>$user_discount,
'account_money'=>$account_money,
'ecv_money'=>$ecv_money,
'ecv_data'=>$ecv_data,
'region_info'=>$region_info,
'is_delivery'=>$is_delivery,
'return_total_score'=>$return_total_score,
'return_total_money'=>$return_total_money,
'paid_account_money'=>$paid_account_money,
'paid_ecv_money'=>$paid_ecv_money
);
$allow_promote = 1;
foreach($goods_list as $k=>$v)
{
$allow_promote = $GLOBALS['db']->getOne('select allow_promote from '.DB_PREFIX.'deal where id = '.$v['deal_id']);
if($allow_promote == 0)
{
break;
}
}
if($allow_promote==1)
{
$promote_list = $GLOBALS['db']->getAll('select * from '.DB_PREFIX.'promote order by sort desc');
foreach($promote_list as $k=>$v)
{
$directory = APP_ROOT_PATH.'system/promote/';
$file = $directory.'/'.$v['class_name'].'_promote.php';
if(file_exists($file))
{
require_once($file);
$promote_class = $v['class_name'].'_promote';
$promote_object = new $promote_class();
$result = $promote_object->count_buy_total($region_id,
$delivery_id,
$payment,
$account_money,
$all_account_money,
$ecvsn,
$ecvpassword,
$goods_list,
$result,
$paid_account_money,
$paid_ecv_money,
$result);
}
}
}
return $result;
}
function count_delivery_fee($weight,$region_id,$delivery_id)
{
$region_id = intval($region_id);
$delivery_id = intval($delivery_id);
$delivery_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'delivery where id = '.$delivery_id);
$delivery_weight_unit = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'weight_unit where id = '.intval($delivery_info['weight_id']));
require_once APP_ROOT_PATH.'system/utils/child.php';
$child = new child('delivery_region');
$delivery_items = $GLOBALS['db']->getAll('select * from '.DB_PREFIX.'delivery_fee where delivery_id = '.intval($delivery_info['id']).' order by id desc');
foreach($delivery_items as $k=>$v)
{
$support_ids = array();
$sp_ids = $v['region_ids'];
$support_ids = explode(',',$sp_ids);
if(in_array($region_id,$support_ids))
{
$delivery_weight_conf = $v;
break;
}
}
if(!$delivery_weight_conf)
{
if($delivery_info['allow_default'] == 1)
{
$delivery_weight_conf = $delivery_info;
}
}
if($delivery_weight_conf)
{
$delivery_weight_conf['first_weight'] = $delivery_weight_conf['first_weight'] * $delivery_weight_unit['rate'];
$delivery_weight_conf['continue_weight'] = $delivery_weight_conf['continue_weight'] * $delivery_weight_unit['rate'];
if($weight <= $delivery_weight_conf['first_weight']) 
{
$delivery_fee = $delivery_weight_conf['first_fee'];
}
else
{
if($delivery_weight_conf['continue_weight']!=0)
$continue_fee = ceil(($weight -$delivery_weight_conf['first_weight']) / $delivery_weight_conf['continue_weight']) * $delivery_weight_conf['continue_fee'];
else
$continue_fee = 0;
$delivery_fee = $delivery_weight_conf['first_fee'] +$continue_fee;
}
}
else
{
$delivery_fee = 0;
}
return $delivery_fee;
}
function make_payment_notice($money,$order_id,$payment_id,$memo='')
{
$notice['create_time'] = get_gmtime();
$notice['order_id'] = $order_id;
$notice['user_id'] = $GLOBALS['db']->getOne('select user_id from '.DB_PREFIX.'deal_order where id = '.$order_id);
$notice['payment_id'] = $payment_id;
$notice['memo'] = $memo;
$notice['money'] = $money;
do{
$notice['notice_sn'] = to_date(get_gmtime(),'Ymdhis').rand(10,99);
$GLOBALS['db']->autoExecute(DB_PREFIX.'payment_notice',$notice,'INSERT','','SILENT');
$notice_id = intval($GLOBALS['db']->insert_id());
}while($notice_id==0);
return $notice_id;
}
function payment_paid($payment_notice_id)
{
$payment_notice_id = intval($payment_notice_id);
$now = get_gmtime();
$GLOBALS['db']->query('update '.DB_PREFIX.'payment_notice set pay_time = '.$now.',is_paid = 1 where id = '.$payment_notice_id.' and is_paid = 0');
$rs = $GLOBALS['db']->affected_rows();
if($rs)
{
$payment_notice = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'payment_notice where id = '.$payment_notice_id);
$payment_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'payment where id = '.$payment_notice['payment_id']);
if($payment_info['class_name'] == 'Voucher')
{
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set pay_amount = pay_amount + '.$payment_notice['money'].',ecv_money = '.$payment_notice['money'].' where id = '.$payment_notice['order_id'].' and is_delete = 0 and order_status = 0 and ((pay_amount + '.$payment_notice['money'].' <= total_price) or '.$payment_notice['money'].'>=total_price)');
$order_incharge_rs = $GLOBALS['db']->affected_rows();
}
elseif($payment_info['class_name'] == 'Account')
{
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set pay_amount = pay_amount + '.$payment_notice['money'].',account_money = account_money + '.$payment_notice['money'].' where id = '.$payment_notice['order_id'].' and is_delete = 0 and order_status = 0 and pay_amount + '.$payment_notice['money'].' <= total_price');
$order_incharge_rs = $GLOBALS['db']->affected_rows();
}
else
{
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set pay_amount = pay_amount + '.$payment_notice['money'].' where id = '.$payment_notice['order_id'].' and is_delete = 0 and order_status = 0 and pay_amount + '.$payment_notice['money'].' <= total_price');
$order_incharge_rs = $GLOBALS['db']->affected_rows();
}
$GLOBALS['db']->query('update '.DB_PREFIX.'payment set total_amount = total_amount + '.$payment_notice['money']." where class_name = '".$payment_info['class_name']."'");
if(!$order_incharge_rs)
{
require_once APP_ROOT_PATH.'system/libs/user.php';
$order_info = $GLOBALS['db']->getRow('select is_delete,order_status from '.DB_PREFIX.'deal_order where id = '.intval($payment_notice['order_id']));
if($order_info['is_delete']==1||$order_info['order_status']==1)
$msg = sprintf($GLOBALS['lang']['DELETE_INCHARGE'],$payment_notice['notice_sn']);
else
$msg = sprintf($GLOBALS['lang']['PAYMENT_INCHARGE'],$payment_notice['notice_sn']);
modify_account(array('money'=>$payment_notice['money'],'score'=>0),$payment_notice['user_id'],$msg);
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set extra_status = 1 where is_delete = 0 and id = '.intval($payment_notice['order_id']));
}
send_payment_sms($payment_notice_id);
send_payment_mail($payment_notice_id);
}
return $rs;
}
function order_paid($order_id)
{
$order_id  = intval($order_id);
$order = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'deal_order where id = '.$order_id);
if($order['pay_amount']>=$order['total_price'])
{
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set pay_status = 2 where id ='.$order_id.' and pay_status <> 2');
$rs = $GLOBALS['db']->affected_rows();
if($rs)
{
order_paid_done($order_id);
$order = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'deal_order where id = '.$order_id);
if($order['pay_status']==2&&$order['after_sale']==0)
$result = true;
else
$result = false;
}
}
elseif($order['pay_amount']<$order['total_price']&&$order['pay_amount']!=0)
{
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set pay_status = 1 where id ='.$order_id);
$result = false;
}
elseif($order['pay_amount']==0)
{
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set pay_status = 0 where id ='.$order_id);
$result = false;
}
return $result;
}
function order_paid_done($order_id)
{
require_once APP_ROOT_PATH.'system/libs/deal.php';
$order_id = intval($order_id);
$stock_status = true;
$order_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'deal_order where id = '.$order_id);
if($order_info['type'] == 0)
{
$order_goods_list = $GLOBALS['db']->getAll('select * from '.DB_PREFIX.'deal_order_item where order_id = '.$order_id);
foreach($order_goods_list as $k=>$v)
{
if($GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'attr_stock where deal_id = '.$v['deal_id']." and locate(attr_str,'".$v['attr_str']."') > 0"))
{
$sql = 'update '.DB_PREFIX.'attr_stock set buy_count = buy_count + '.$v['number'].' where deal_id = '.$v['deal_id'].
' and ((buy_count + '.$v['number'].' <= stock_cfg) or stock_cfg = 0 )'.
" and locate(attr_str,'".$v['attr_str']."') > 0 ";
$GLOBALS['db']->query($sql);
$rs = $GLOBALS['db']->affected_rows();
if($rs)
{
$affect_attr_list[] = $v;
}
else
{
$stock_status = false;
break;
}
}
}
if($stock_status)
{
$goods_list = $GLOBALS['db']->getAll('select deal_id,sum(number) as num from '.DB_PREFIX.'deal_order_item where order_id = '.$order_id.' group by deal_id');
foreach($goods_list as $k=>$v)
{
$sql = 'update '.DB_PREFIX.'deal set buy_count = buy_count + '.$v['num'].
',user_count = user_count + 1 where id='.$v['deal_id'].
' and ((buy_count + '.$v['num'].'<= max_bought) or max_bought = 0) '.
' and time_status = 1 and buy_status <> 2';
$GLOBALS['db']->query($sql);
$rs = $GLOBALS['db']->affected_rows();
if($rs)
{
$affect_list[] = $v;
}
else
{
$stock_status = false;
break;
}
}
}
if($stock_status)
{
foreach($goods_list as $k=>$v)
{
$deal_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'deal where id = '.intval($v['deal_id']));
if($deal_info['is_coupon'] == 1)
{
if($deal_info['deal_type'] == 1) 
{
$deal_order_item_list = $GLOBALS['db']->getAll('select * from '.DB_PREFIX.'deal_order_item where order_id = '.$order_info['id'].' and deal_id = '.$v['deal_id']);
foreach($deal_order_item_list as $item)
{
$sql = 'update '.DB_PREFIX.'deal_coupon set user_id='.$order_info['user_id'].
',order_id = '.$order_info['id'].
',order_deal_id = '.$item['id'].
' where deal_id = '.$v['deal_id'].
' and user_id = 0 '.
' and is_delete = 0';
$GLOBALS['db']->query($sql);
$exist_coupon = $GLOBALS['db']->affected_rows();
if(!$exist_coupon)
{
add_coupon($v['deal_id'],$order_info['user_id'],0,'','',0,0,$item['id'],$order_info['id']);
}
}
}
else
{
$deal_order_item_list = $GLOBALS['db']->getAll('select * from '.DB_PREFIX.'deal_order_item where order_id = '.$order_info['id'].' and deal_id = '.$v['deal_id']);
foreach($deal_order_item_list as $item)
{
for($i=0;$i<$item['number'];$i++) 
{
$sql = 'update '.DB_PREFIX.'deal_coupon set user_id='.$order_info['user_id'].
',order_id = '.$order_info['id'].
',order_deal_id = '.$item['id'].
' where deal_id = '.$v['deal_id'].
' and user_id = 0 '.
' and is_delete = 0 limit 1';
$GLOBALS['db']->query($sql);
$exist_coupon = $GLOBALS['db']->affected_rows();
if(!$exist_coupon)
{
add_coupon($v['deal_id'],$order_info['user_id'],0,'','',0,0,$item['id'],$order_info['id']);
}
}
}
}
}
}
require_once APP_ROOT_PATH.'system/libs/user.php';
if($order_info['return_total_money']!=0)
{
$msg = sprintf($GLOBALS['lang']['ORDER_RETURN_MONEY'],$order_info['order_sn']);
modify_account(array('money'=>$order_info['return_total_money'],'score'=>0),$order_info['user_id'],$msg);
}
if($order_info['return_total_score']!=0)
{
$msg = sprintf($GLOBALS['lang']['ORDER_RETURN_SCORE'],$order_info['order_sn']);
modify_account(array('money'=>0,'score'=>$order_info['return_total_score']),$order_info['user_id'],$msg);
}
$user_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'user where id = '.$order_info['user_id']);
$is_referrals = 1;
foreach($goods_list as $k=>$v)
{
$is_referrals = $GLOBALS['db']->getOne('select is_referral from '.DB_PREFIX.'deal where id = '.$v['deal_id']);
if($is_referrals == 0)
{
break;
}
}
if($user_info['referral_count']<app_conf('REFERRAL_LIMIT')&&$is_referrals == 1)
{
$parent_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'user where id = '.$user_info['pid']);
if($parent_info)
{
if((app_conf('REFERRAL_IP_LIMIT')==1&&$parent_info['login_ip']!=get_client_ip())||app_conf('REFERRAL_IP_LIMIT')==0) 
{
if(app_conf('INVITE_REFERRALS_TYPE')==0) 
{
$referral_data['user_id'] = $parent_info['id'];
$referral_data['rel_user_id'] = $user_info['id'];
$referral_data['create_time'] = get_gmtime();
$referral_data['money']	=	app_conf('INVITE_REFERRALS');
$referral_data['order_id']	=	$order_info['id'];
$GLOBALS['db']->autoExecute(DB_PREFIX.'referrals',$referral_data);
}
else
{
$referral_data['user_id'] = $parent_info['id'];
$referral_data['rel_user_id'] = $user_info['id'];
$referral_data['create_time'] = get_gmtime();
$referral_data['score']	=	app_conf('INVITE_REFERRALS');
$referral_data['order_id']	=	$order_info['id'];
$GLOBALS['db']->autoExecute(DB_PREFIX.'referrals',$referral_data);
}
$GLOBALS['db']->query('update '.DB_PREFIX.'user set referral_count = referral_count + 1 where id = '.$user_info['id']);
}
}
}
if($order_info['pay_amount']>$order_info['total_price'])
{
require_once APP_ROOT_PATH.'system/libs/user.php';
if($order_info['total_price']<0)
$msg = sprintf($GLOBALS['lang']['MONEYORDER_INCHARGE'],$order_info['order_sn']);
else
$msg = sprintf($GLOBALS['lang']['OUTOFMONEY_INCHARGE'],$order_info['order_sn']);
$refund_money = $order_info['pay_amount']-$order_info['total_price'];
if($order_info['account_money']>$refund_money)$account_money_now = $order_info['account_money'] -$refund_money;else $account_money_now = 0;
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set account_money = '.$account_money_now.' where id = '.$order_info['id']);
if($order_info['ecv_money']>$refund_money)$ecv_money_now = $order_info['ecv_money'] -$refund_money;else $ecv_money_now = 0;
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set ecv_money = '.$ecv_money_now.' where id = '.$order_info['id']);
modify_account(array('money'=>($order_info['pay_amount']-$order_info['total_price']),'score'=>0),$order_info['user_id'],$msg);
}
$lottery_list = $GLOBALS['db']->getAll('select d.id as did,doi.number from '.DB_PREFIX.'deal_order_item as doi left join '.DB_PREFIX.'deal_order as do on doi.order_id = do.id left join '.DB_PREFIX.'deal as d on doi.deal_id = d.id where d.is_lottery = 1 and do.id = '.$order_info['id']);
$lottery_user = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'user where id = '.intval($order_info['user_id']));
$lottery_count = $GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'lottery where user_id = '.intval($order_info['user_id']));
if($lottery_count == 0&&$lottery_user['pid']!=0)
{
$lottery_puser = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'user where id = '.intval($lottery_user['pid']));
foreach($lottery_list as $lottery)
{
$k = 0;
do{
if($k>10)break;
$buy_count = $GLOBALS['db']->getOne('select buy_count from '.DB_PREFIX.'deal where id = '.$lottery['did']);
$max_sn = $buy_count -$lottery['number'] +intval($GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'lottery where deal_id = '.intval($lottery['did']).' and buyer_id <> 0 '));
$sn = $max_sn +1;
$sn = str_pad($sn,'6','0',STR_PAD_LEFT);
$sql = 'insert into '.DB_PREFIX."lottery (`lottery_sn`,`deal_id`,`user_id`,`mobile`,`create_time`,`buyer_id`) select '".$sn."','".$lottery['did']."',".$lottery_puser['id'].",'".$lottery_puser['lottery_mobile']."',".get_gmtime().','.$order_info['user_id'].' from dual where not exists( select * from '.DB_PREFIX.'lottery where deal_id = '.$lottery['did']." and lottery_sn = '".$sn."')";
$GLOBALS['db']->query($sql);
send_lottery_sms(intval($GLOBALS['db']->insert_id()));
$k++;
}while(intval($GLOBALS['db']->insert_id())==0);
}
}
foreach($lottery_list as $lottery)
{
for($i=0;$i<$lottery['number'];$i++) 
{
$k = 0;
do{
if($k>10)break;
$buy_count = $GLOBALS['db']->getOne('select buy_count from '.DB_PREFIX.'deal where id = '.$lottery['did']);
$max_sn = $buy_count -$lottery['number'] +intval($GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'lottery where deal_id = '.intval($lottery['did']).' and buyer_id <> 0 '));
$sn = $max_sn +$i +1;
$sn = str_pad($sn,'6','0',STR_PAD_LEFT);
$sql = 'insert into '.DB_PREFIX."lottery (`lottery_sn`,`deal_id`,`user_id`,`mobile`,`create_time`,`buyer_id`) select '".$sn."','".$lottery['did']."',".$order_info['user_id'].','.$lottery_user['lottery_mobile'].','.get_gmtime().',0 from dual where not exists( select * from '.DB_PREFIX.'lottery where deal_id = '.$lottery['did']." and lottery_sn = '".$sn."')";
$GLOBALS['db']->query($sql);
send_lottery_sms(intval($GLOBALS['db']->insert_id()));
$k++;
}while(intval($GLOBALS['db']->insert_id())==0);
}
}
}
else
{
foreach($affect_attr_list as $k=>$v)
{
$sql = 'update '.DB_PREFIX.'attr_stock set buy_count = buy_count - '.$v['number'].' where deal_id = '.$v['deal_id'].
" and locate(attr_str,'".$v['attr_str']."') > 0 ";
$GLOBALS['db']->query($sql);
}
foreach($affect_list as $k=>$v)
{
$sql = 'update '.DB_PREFIX.'deal set buy_count = buy_count - '.$v['num'].
',user_count = user_count - 1 where id='.$v['deal_id'];
$GLOBALS['db']->query($sql);
}
require_once APP_ROOT_PATH.'system/libs/user.php';
$msg = sprintf($GLOBALS['lang']['OUTOFSTOCK_INCHARGE'],$order_info['order_sn']);
modify_account(array('money'=>$order_info['total_price'],'score'=>0),$order_info['user_id'],$msg);
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set extra_status = 2, after_sale = 1, refund_money = pay_amount, order_status = 1 where id = '.intval($order_info['id']));
$log['log_info'] = $msg;
$log['log_time'] = get_gmtime();
$log['order_id'] = intval($order_info['id']);
$GLOBALS['db']->autoExecute(DB_PREFIX.'deal_order_log',$log);
}
syn_dealing();
}
else
{
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_order set order_status = 1 where id = '.$order_info['id']);
require_once APP_ROOT_PATH.'system/libs/user.php';
$msg = sprintf($GLOBALS['lang']['USER_INCHARGE_DONE'],$order_info['order_sn']);
modify_account(array('money'=>$order_info['total_price']-$order_info['payment_fee'],'score'=>0),$order_info['user_id'],$msg);
}
}
function syn_cart()
{
$first_row = $GLOBALS['db']->getRow('select dc.*,d.cart_type as cart_type from '.DB_PREFIX.'deal_cart as dc left join '.DB_PREFIX."deal as d on dc.deal_id = d.id where dc.session_id = '".es_session::id()."' and dc.user_id = ".intval($GLOBALS['user_info']['id']).' order by dc.create_time desc');
$result = $GLOBALS['db']->getAll('select dc.id,dc.deal_id,dc.supplier_id from '.DB_PREFIX.'deal_cart as dc left join '.DB_PREFIX."deal as d on dc.deal_id = d.id where dc.session_id = '".es_session::id()."' and dc.user_id = ".intval($GLOBALS['user_info']['id']).' and d.cart_type = 3');
if($result)
{
if($first_row['cart_type']==3)
{
$GLOBALS['db']->query('delete from '.DB_PREFIX."deal_cart where session_id = '".es_session::id()."' and user_id = ".intval($GLOBALS['user_info']['id']).' and id <> '.$first_row['id']);
return;
}
else
{
$ids = array(0);
foreach($result as $row)
{
array_push($ids,$row['id']);
}
$GLOBALS['db']->query('delete from '.DB_PREFIX."deal_cart where session_id = '".es_session::id()."' and user_id = ".intval($GLOBALS['user_info']['id']).' and id in ('.implode(',',$ids).')');
return;
}
}
$result = $GLOBALS['db']->getAll('select dc.id,dc.deal_id,dc.supplier_id from '.DB_PREFIX.'deal_cart as dc left join '.DB_PREFIX."deal as d on dc.deal_id = d.id where dc.session_id = '".es_session::id()."' and dc.user_id = ".intval($GLOBALS['user_info']['id']).' and d.cart_type = 2');
if($result)
{
if($first_row['cart_type']==2)
{
$GLOBALS['db']->query('delete from '.DB_PREFIX."deal_cart where session_id = '".es_session::id()."' and user_id = ".intval($GLOBALS['user_info']['id']).' and supplier_id <> '.$first_row['supplier_id']);
return;
}
else
{
$ids = array(0);
foreach($result as $row)
{
array_push($ids,$row['supplier_id']);
}
$GLOBALS['db']->query('delete from '.DB_PREFIX."deal_cart where session_id = '".es_session::id()."' and user_id = ".intval($GLOBALS['user_info']['id']).' and supplier_id in ('.implode(',',$ids).')');
return;
}
}
$result = $GLOBALS['db']->getAll('select dc.id,dc.deal_id,dc.supplier_id from '.DB_PREFIX.'deal_cart as dc left join '.DB_PREFIX."deal as d on dc.deal_id = d.id where dc.session_id = '".es_session::id()."' and dc.user_id = ".intval($GLOBALS['user_info']['id']).' and d.cart_type = 1');
if($result)
{
if($first_row['cart_type']==1)
{
$GLOBALS['db']->query('delete from '.DB_PREFIX."deal_cart where session_id = '".es_session::id()."' and user_id = ".intval($GLOBALS['user_info']['id']).' and deal_id <> '.$first_row['deal_id']);
return;
}
else
{
$ids = array(0);
foreach($result as $row)
{
array_push($ids,$row['deal_id']);
}
$GLOBALS['db']->query('delete from '.DB_PREFIX."deal_cart where session_id = '".es_session::id()."' and user_id = ".intval($GLOBALS['user_info']['id']).' and deal_id in ('.implode(',',$ids).')');
return;
}
}
}

?>